package com.itheima.a02polymorphismDemo2;

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        // 类型转换
        /*if(a instanceof Dog){
            Dog d = (Dog) a;
            d.lookHome();
        }else if(a instanceof Cat){
            Cat c = (Cat) a;
            c.catchMouse();
        }else{
            System.out.println("没有这个类型");
        }*/

        // JDK14新特性
        if(a instanceof Dog d){
            d.lookHome();
        }else if(a instanceof Cat c){
            c.catchMouse();
        }else{
            System.out.println("没有这个类型");
        }
    }
}

class Animal{
    String name = "Animal";

    public void eat(){
        System.out.println("Animal eating");
    }
}

class Dog extends Animal {
    String name = "Dog";

    @Override
    public void eat() {
        System.out.println("Dog -- eating");
    }

    public void lookHome(){
        System.out.println("Looking Home");
    }
}

class Cat extends Animal{
    String name = "Cat";

    @Override
    public void eat() {
        System.out.println("Cat -- eating");
    }

    public void catchMouse(){
        System.out.println("Catching Mouse");
    }
}
